package com.liflag.music.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liflag.music.model.AppSong;
import com.liflag.music.service.AppSongService;
import com.liflag.music.util.KuwoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 歌曲Service业务层处理
 * 
 * @author dafeiyu
 * @date 2021-08-10
 */
@Service
public class AppSongServiceImpl implements AppSongService {

    /**
     * 查询歌曲
     * @param id 歌曲ID
     * @return 歌曲
     */
    @Override
    public AppSong selectAppSongById(Long id)
    {
        return null;
    }

    /**
     * 查询歌曲列表
     *
     * @param appSong 歌曲
     * @return 歌曲
     */
    @Override
    public List<AppSong> selectAppSongList(AppSong appSong)
    {
        return null;
    }

    /**
     * 新增歌曲
     * 
     * @param appSong 歌曲
     * @return 结果
     */
    @Override
    public int insertAppSong(AppSong appSong)
    {
        return 0;
    }

    /**
     * 修改歌曲
     * 
     * @param appSong 歌曲
     * @return 结果
     */
    @Override
    public int updateAppSong(AppSong appSong)
    {
        return 0;
    }

    /**
     * 删除歌曲对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAppSongByIds(String ids)
    {
        return 0;
    }

    /**
     * 删除歌曲信息
     * 
     * @param id 歌曲ID
     * @return 结果
     */
    @Override
    public int deleteAppSongById(Long id)
    {
        return 0;
    }

    @Override
    public AppSong queryByMid(int rid) {
        return null;
    }

    @Override
    public List<AppSong> queryByHotLimit() {
        return null;
    }

    @Override
    public List<HashMap> queryByListId(String listId) {
        List tracks = new ArrayList();
        List<AppSong> songs = null;
        for(int i=0;i<songs.size();i++) {
            HashMap mitem = new HashMap();
            mitem.put("name",songs.get(i).getName());// 艺术家名字
            mitem.put("artist",songs.get(i).getSinger());// 专辑名字
            mitem.put("source","kuwo");
            mitem.put("pic",songs.get(i).getPic());//// 专辑图片
            mitem.put("picUrl",songs.get(i).getPic());//// 专辑图片
            mitem.put("pic_id", songs.get(i).getMid());
            mitem.put("url_id", songs.get(i).getMid());
            mitem.put("lyric_id", songs.get(i).getMid());
            mitem.put("id", songs.get(i).getMid());//// 歌词ID
            tracks.add(mitem);
        }
        return tracks;
    }

    @Override
    public HashMap querySongList(String listId) {
        HashMap res = new HashMap();
        return res;
    }

    @Override
    public JSONArray getByKey(String key,String page) {
        JSONArray res = new JSONArray();
        JSONObject jsonObject= JSON.parseObject(KuwoUtil.getres(key,page));
        JSONObject data = jsonObject.getJSONObject("data");
        if(data==null||data.equals(null)){
            return null;
            //do nothing
        } else {
            JSONArray list = data.getJSONArray("list");
            int len = list.size();
            for (int i = 0; i < len; i++) {
                JSONObject jtp = list.getJSONObject(i);
                JSONObject jitem = new JSONObject();
                jitem.put("name", jtp.get("name"));
                jitem.put("artist", jtp.get("artist"));
                jitem.put("picUrl", jtp.get("pic"));//// 专辑图片
                jitem.put("pic_id",jtp.get("rid"));//// 专辑图片
                jitem.put("url_id", jtp.get("rid"));
                jitem.put("lyric_id", jtp.get("rid"));
                jitem.put("album", jtp.get("album"));
                jitem.put("id", jtp.get("rid"));
                res.add(jitem);
            }
            return res;
        }
    }
}
